package com.gpt.article.service.impl.taskHandle.testTask;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelWriter;

import java.io.File;
import java.util.*;

/**
 * 用于单独拆分excel
 */
public class SplitExcek {

    public static String excelPath = "C:\\Users\\竹子\\Desktop\\content\\yingyu\\英语_all.xlsx";
    public static String outExcelPath = "C:\\Users\\竹子\\Desktop\\content\\yingyu\\英语_split.xlsx";


    public static void main(String[] args) {
        List<Map<String, Object>> duibimaps = readExcelUtil();
//        "Name", "Catalogue", "SummaryA", "PRT Words", "ID"
        //有序List map


        ArrayList<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
        Map<String,Object> keyMap = null;
        for(Map<String, Object> map:duibimaps){
            String name = map.get("Name").toString();
            String catalogue = map.get("Catalogue").toString();
            String summaryA = map.get("SummaryA").toString()+String.valueOf(map.get("SummaryA_ex"));
            System.out.println(map+"\n\n~~~~~~~\n");
            String id = map.get("ID").toString();
            //处理summaryA 字段 分割成多个
            String[] summaryAs = summaryA.split("<h3 id=\"section");
            int sno = 1;
            keyMap = new  LinkedHashMap<String,Object>();
            keyMap.put("Name",name);
            keyMap.put("Catalogue",catalogue);
            for(String summaryA1:summaryAs){
                if(summaryA1.length()>0){
                    summaryA1 = "<h3 id=\"section"+summaryA1;
                    keyMap.put("SummaryA"+sno,summaryA1);
                    sno++;
                }
            }
//            row.put("PRT Words",prtWords);
            keyMap.put("ID",id);
            rows.add(keyMap);
        }
        writeExcel(rows);
    }


    public static List<Map<String, Object>> readExcelUtil(){
        File duibiExccelFile = new File(excelPath);
        ExcelReader duibireader = cn.hutool.poi.excel.ExcelUtil.getReader(duibiExccelFile);
        List<Map<String, Object>> duibimaps = duibireader.readAll();
        return duibimaps;
    }

    public static Boolean writeExcel(List<Map<String, Object>> mapRows){
        //创建excel
        ExcelWriter writer = cn.hutool.poi.excel.ExcelUtil.getWriter(outExcelPath);
// 一次性写出内容，使用默认样式，强制输出标题
        writer.write(mapRows, true);
// 关闭writer，释放内存
        writer.close();

        return true;
    }
}
